package com.wr.config;

import com.wr.pojo.Configuration;
import com.wr.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.EventListener;
import java.util.List;
import java.util.Map;

/**
 * @author 王瑞
 * @Description 对Mapper.xml文件进行解析:
 * @date 2020/1/29 - 20:32
 */
public class XmlMapperBuilder {

    private Configuration configuration;

    public XmlMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void pase(InputStream inputStream) throws DocumentException {
        Document read = new SAXReader().read(inputStream);
        Element rootElement = read.getRootElement();
        List<Element> list = rootElement.selectNodes("//select");
        String namespace = rootElement.attributeValue("namespace");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String textTrim = element.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParameterType(parameterType);
            mappedStatement.setSql(textTrim);
            mappedStatement.setResultType(resultType);
            Map<String, MappedStatement> map = configuration.getMap();
            map.put(namespace+"."+id,mappedStatement);
        }

    }
}

